home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / pipes.zip / SERVER.C < prev    next >
C/C++ Source or Header  |  1993-11-24  |  10KB  |  296 lines

  1. /********************************************************************\
  2. *  Julie Solon                                 *
  3. *  Microsoft Developer Support                                       *
  4. *  Copyright (c) 1992, 1993 Microsoft Corporation                    *
  5. *                                                                    *
  6. *  Comments:                                                         *
  7. *                                                                    *
  8. *  Functions:                                 *
  9. *                                                                    *
  10. *                                     *
  11. \********************************************************************/
  12.  
  13.  
  14. /*********************  Header Files  *********************/
  15.  
  16. #include <windows.h>
  17. #include <string.h>
  18. #include "server.h"
  19.  
  20. /**********************  Defines  *************************/
  21.  
  22. #define BUFSIZE 25
  23.  
  24. /*********************  Prototypes  ***********************/
  25.  
  26. LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
  27. LRESULT WINAPI AboutDlgProc( HWND, UINT, WPARAM, LPARAM );
  28. LRESULT WINAPI TestFunc( DWORD * );
  29.  
  30. /*******************  Global Variables ********************/
  31.  
  32. HANDLE ghInstance;
  33. char Buffer1[BUFSIZE];
  34. char Buffer2[BUFSIZE];
  35.  
  36.  
  37. /********************************************************************\
  38. *  Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int)    *
  39. *                                                                    *
  40. *   Purpose: Initializes Application                                 *
  41. *                                                                    *
  42. *  Comments: Standard template                                       *
  43. *                                                                    *
  44. *                                                                    *
  45. \********************************************************************/
  46.  
  47.  
  48. int PASCAL WinMain( HINSTANCE hInstance,
  49.             HINSTANCE hPrevInstance,
  50.             LPSTR lpszCmdLine,
  51.             int nCmdShow )
  52. {
  53.    WNDCLASS wc;
  54.    MSG msg;
  55.    HWND hWnd;
  56.  
  57.    if( !hPrevInstance ) {
  58.       wc.lpszClassName = "ServerClass";
  59.       wc.lpfnWndProc = MainWndProc;
  60.       wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
  61.       wc.hInstance = hInstance;
  62.       wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  63.       wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  64.       wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 );
  65.       wc.lpszMenuName = "ServerMenu";
  66.       wc.cbClsExtra = 0;
  67.       wc.cbWndExtra = 0;
  68.  
  69.       RegisterClass( &wc );
  70.    }
  71.  
  72.    ghInstance = hInstance;
  73.  
  74.    hWnd = CreateWindow( "ServerClass",
  75.             "Server Application",
  76.                         WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,
  77.                         0,
  78.                         0,
  79.                         CW_USEDEFAULT,
  80.                         CW_USEDEFAULT,
  81.                         NULL,
  82.                         NULL,
  83.                         hInstance,
  84.                         NULL
  85.                       );
  86.  
  87.    ShowWindow( hWnd, nCmdShow );
  88.  
  89.    while( GetMessage( &msg, NULL, 0, 0 ) ) {
  90.       TranslateMessage( &msg );
  91.       DispatchMessage( &msg );
  92.    }
  93.  
  94.    return msg.wParam;
  95. }
  96.  
  97.  
  98. /********************************************************************\
  99. * Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) *
  100. *                                                                    *
  101. *  Purpose: Processes Application Messages                           *
  102. *                                                                    *
  103. * Comments: The following messages are processed                     *
  104. *                                                                    *
  105. *           WM_CREATE                                                *
  106. *           WM_HSCROLL                                               *
  107. *           WM_VSCROLL                                               *
  108. *           WM_KEYDOWN                                               *
  109. *           WM_PAINT                                                 *
  110. *           WM_COMMAND                                               *
  111. *           WM_DESTROY                                               *
  112. *                                                                    *
  113. *                                                                    *
  114. \********************************************************************/
  115.  
  116.  
  117. LRESULT CALLBACK MainWndProc( HWND hWnd, UINT msg, WPARAM wParam,
  118.    LPARAM lParam )
  119. {
  120.    PAINTSTRUCT ps;
  121.    HDC hDC;
  122.  
  123.    DWORD dwArg;
  124.    DWORD ThreadID;
  125.  
  126.    STARTUPINFO si;
  127.    PROCESS_INFORMATION pi;
  128.  
  129.    switch( msg ) {
  130.  
  131. /**************************************************************\
  132. *     WM_CREATE:                           *
  133. \**************************************************************/
  134.  
  135.       case WM_CREATE:
  136.          strcpy( Buffer1, "This is a test" );
  137.          strcpy( Buffer2, "This is another test" );
  138.  
  139.          memset( &si, 0, sizeof(STARTUPINFO) );
  140.          si.lpReserved = NULL;
  141.          si.lpReserved2 = NULL;
  142.  
  143.          CreateProcess( "cli16.exe",
  144.             NULL,
  145.             NULL,
  146.             NULL,
  147.             TRUE,
  148.             NORMAL_PRIORITY_CLASS,
  149.             NULL,
  150.             NULL,
  151.             &si,
  152.             &pi
  153.          );
  154.      break;
  155.  
  156. /**************************************************************\
  157. *     WM_PAINT:                            *
  158. \**************************************************************/
  159.  
  160.       case WM_PAINT:
  161.          hDC = BeginPaint( hWnd, &ps );
  162.  
  163.      EndPaint( hWnd, &ps );
  164.          break;
  165.  
  166. /**************************************************************\
  167. *     WM_COMMAND:                           *
  168. \**************************************************************/
  169.  
  170.       case WM_COMMAND:
  171.          switch( wParam ) {
  172.             case IDM_TEST1:
  173.                dwArg = 1;
  174.                CreateThread(  NULL,
  175.                                  0,
  176.  (LPTHREAD_START_ROUTINE) TestFunc,
  177.                             &dwArg,
  178.                                  0,
  179.                          &ThreadID
  180.                );
  181.                break;
  182.             case IDM_TEST2:
  183.                dwArg = 2;
  184.                CreateThread(  NULL,
  185.                                  0,
  186.  (LPTHREAD_START_ROUTINE) TestFunc,
  187.                             &dwArg,
  188.                                  0,
  189.                          &ThreadID
  190.                );
  191.                break;
  192.  
  193.         case IDM_ABOUT:
  194.                DialogBox( ghInstance, "AboutDlg", hWnd, (DLGPROC)  
  195.                              AboutDlgProc );
  196.                break;
  197.          }
  198.          break;
  199.  
  200. /**************************************************************\
  201. *     WM_DESTROY: PostQuitMessage() is called                  *
  202. \**************************************************************/
  203.  
  204.       case WM_DESTROY:
  205.          PostQuitMessage( 0 );
  206.          break;
  207.  
  208. /**************************************************************\
  209. *     Let the default window proc handle all other messages    *
  210. \**************************************************************/
  211.  
  212.       default:
  213.          return( DefWindowProc( hWnd, msg, wParam, lParam ));
  214.    }
  215.  
  216.    return 0;
  217. }
  218.  
  219. /********************************************************************\
  220. * Function: LRESULT CALLBACK AboutDlgProc(HWND, UINT, WPARAM, LPARAM)*
  221. *                                                                    *
  222. *  Purpose: Processes "About" Dialog Box Messages                    *
  223. *                                                                    *
  224. * Comments: The Dialog Box is displayed when the user selects        *
  225. *           Help.About.                                              *
  226. *                                                                    *
  227. \********************************************************************/
  228.  
  229.  
  230. LRESULT CALLBACK AboutDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
  231. {
  232.    switch( uMsg ) {
  233.       case WM_INITDIALOG:
  234.          return TRUE;
  235.       case WM_COMMAND:
  236.          switch( wParam ) {
  237.             case IDOK:
  238.                EndDialog( hDlg, TRUE );
  239.                return TRUE;
  240.          }
  241.       break;
  242.    }
  243.  
  244.    return FALSE;
  245. }
  246.  
  247. /*******************************************************************\
  248. * Function: LRESTULT WINAPI TestFunc( DWORD )                       *
  249. *                                                                   *
  250. *  Purpose: Thread Function                                         *
  251. *                                                                   *
  252. * Comments: User selects menu option Test.Test1 or Test.Test2, a    *
  253. *           thread is started which creates a pipe and puts a       *
  254. *           message in it                                           *
  255. *                                                                   *
  256. \*******************************************************************/
  257.  
  258.  
  259. LRESULT WINAPI TestFunc( DWORD *dwArg )
  260. {
  261.    char Buffer[BUFSIZE];
  262.  
  263.    HANDLE hPipe;
  264.    DWORD dwBytes;
  265.    BOOL fConnected;
  266.  
  267.    if( *dwArg == 1 )
  268.       lstrcpy( Buffer, Buffer1 );
  269.    else lstrcpy( Buffer, Buffer2 );
  270.  
  271.    hPipe = CreateNamedPipe( "\\\\.\\pipe\\TestPipe",
  272.       PIPE_ACCESS_OUTBOUND,
  273.       PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
  274.       1,
  275.       BUFSIZE,
  276.       BUFSIZE,
  277.       NMPWAIT_WAIT_FOREVER,
  278.       NULL
  279.    );
  280.    fConnected = ConnectNamedPipe( hPipe, NULL );
  281.    if( fConnected ) {
  282.       WriteFile( hPipe,
  283.       Buffer,
  284.       lstrlen( Buffer ),
  285.       &dwBytes,
  286.       NULL
  287.       );
  288.    }
  289.    FlushFileBuffers( hPipe );
  290.    DisconnectNamedPipe( hPipe );
  291.  
  292.    CloseHandle( hPipe );
  293.    ExitThread( 1 );
  294.    return TRUE;
  295. }
  296.